package com.lenovo.smartengine.service;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.IBinder;
import android.util.Log;
import com.dolby.api.DsAccess;
import com.dolby.api.DsConstants;
import com.dolby.api.DsGlobalEx;
import com.dolby.api.IDsAccessEvents;
import com.dolby.api.IDsEvents;
import com.lenovo.smartengine.receiver.IncomingAudioTypeReceiverEngine;

public class SmartEngineService extends Service implements IDsAccessEvents,IDsEvents{

	private final String TAG = "MainActivity";
    private DsGlobalEx mDsClient = null;
	private IntentFilter filter = null;
	
	@Override
	public void onCreate() {
		Log.i(TAG, "ExampleService-onCreate");
		super.onCreate();
		
		Context context= this.getApplicationContext();
		try{
			if (mDsClient == null) {
				mDsClient = new DsGlobalEx();
		        // bind to DsService
		        boolean isSuccess = mDsClient.registerClient(context, this);
		        Log.i(TAG,"isSuccess:"+isSuccess);
		    }
		}catch(Exception e){
			mDsClient = null;
		}


		filter = new IntentFilter("com.lenovo.action.audiotype");
		registerReceiver(new IncomingAudioTypeReceiverEngine(mDsClient), filter);
	}

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		// 执行文件的下载或者播放等操作
		Log.i(TAG, "ExampleService-onStartCommand");
		/*
		 * 这里返回状态有三个值，分别是:
		 * 1、START_STICKY：当服务进程在运行时被杀死，系统将会把它置为started状态，但是不保存其传递的Intent对象
		 * ，之后，系统会尝试重新创建服务;
		 * 2、START_NOT_STICKY：当服务进程在运行时被杀死，并且没有新的Intent对象传递过来的话，
		 * 系统将会把它置为started状态， 但是系统不会重新创建服务，直到startService(Intent intent)方法再次被调用;
		 * 3、START_REDELIVER_INTENT：当服务进程在运行时被杀死，它将会在隔一段时间后自动创建，
		 * 并且最后一个传递的Intent对象将会再次传递过来。
		 */
		return START_STICKY;
	}

	@Override
	public IBinder onBind(Intent intent) {
		Log.i(TAG, "ExampleService-onBind");
		return null;
	}

	@Override
	public void onDestroy() {
		Log.i(TAG, "ExampleService-onDestroy");
		super.onDestroy();
	}

	@Override
	public void onAccessAvailable() {
		// TODO Auto-generated method stub
	      Log.d(TAG, "onAccessAvailable() ------------------");
	        if ((null != mDsClient) && (DsAccess.THIS_APP_GRANTED != mDsClient.checkAccessRight())) {
	            if (DsConstants.DS_NO_ERROR != mDsClient.requestAccessRight()) {
	                Log.d(TAG, "Fail to get access right");
	                return;
	            }
	        }
	        Log.d(TAG, "Got access right");
	}

	@Override
	public void onAccessForceReleased(String arg0, int arg1) {
		// TODO Auto-generated method stub
        Log.d(TAG, "onAccessForceReleased() ------------------");
	}

	@Override
	public boolean onAccessRequested(String arg0, int arg1) {
		// TODO Auto-generated method stub
	       Log.d(TAG, "onAccessRequested() ------------------");
		return false;
	}

	@Override
	public void onClientConnected() {
		// TODO Auto-generated method stub
		 Log.d(TAG, "onClientConnected() ------------------");

	        // register Ds Event listener
	        mDsClient.registerDsEvents(this);
	        Log.d(TAG, "Register DsEvent listener");

	        int result = mDsClient.requestAccessRight();
	        if (result != DsConstants.DS_NO_ERROR)
	        {
	            Log.d(TAG, "onClientConnected() request audio focuse failed, result = " + result);

	            // DS2API-TODO: ......??? When and Where should application request audio focus again???

	            return;
	        } else {
	            Log.d(TAG, "onClientConnected() request audio focuse successfully!");
	        }
	        // Client has connected.
	}

	@Override
	public void onClientDisconnected() {
		// TODO Auto-generated method stub
        Log.d(TAG, "onClientDisconnected()");
	}
	
	@Override
	public void onDsOn(boolean on) {
		// TODO Auto-generated method stub
		Log.d(TAG,"onDsOn(" + on + ")");
	}

	@Override
	public void onDsSuspended(boolean isSuspended) {
		// TODO Auto-generated method stub
        Log.d(TAG,"onDsSuspended(" + isSuspended + ")");
	}

	@Override
	public void onProfileSelected(int profile) {
		// TODO Auto-generated method stub
		Log.d(TAG,"onProfileSelected(" + profile + ")");
	}

	@Override
	public void onProfileSettingsChanged(int profile) {
		// TODO Auto-generated method stub
	      Log.d(TAG,"onProfileSettingsChanged(" + profile + ")");
	}
}
